package component

import (
	"gitee.com/liukexing/mqbroker/core/driver/logger"
	"gitee.com/liukexing/mqbroker/core/server/pubsub"
	"gitee.com/liukexing/mqbroker/core/typedef"
	"github.com/robfig/cron"
	"strings"
)

func InitTask(info typedef.ModuleInfo ,Task *cron.Cron,tasklist map[string]typedef.TaskInfo) {
	if Task != nil {
		Task.Stop()
	}
	Task = cron.New()
	for k, v := range tasklist {
		logger.CheckErr(typedef.ErrInfo{
			Info: info,
			Err:  Task.AddJob(strings.Split(k,"::")[1], &TaskJob{Info: info, Task: v}),
		})
	}
	Task.Start()
}

type TaskJob struct {
	Task typedef.TaskInfo
	Info typedef.ModuleInfo
}

func (t *TaskJob) Run() {
	if t.Task.Handle != nil {
		logger.CheckErr(typedef.ErrInfo{
			Info: t.Info,
			Err:  t.Task.Handle(t.Task.Arg),
		})
	}
}

func FindRegister(info typedef.ModuleInfo) (string, bool) {
	return pubsub.CommandMsgPub(info.ID,"comlookupd", "Find", nil)
}
